#pragma once

#include <iostream>
#include <unordered_map>
#include <string>
#include <fstream>

static const std::string gdefault_dict = "dict.txt";
static const std::string gsep = ": ";

class Dict
{
public:
    Dict()
    {
        std::ifstream file(gdefault_dict); // 打开文件
        std::string line;
        std::string chinese;
        std::string english;

        if (!file.is_open())
        {
            std::cout << "Unable to open file" << std::endl;
        }

        while (std::getline(file, line))
        {                           
            //字符串分割
            auto pos = line.find(gsep);
            if(pos != std::string::npos)
            {
                english = line.substr(0,pos);
                chinese = line.substr(pos+gsep.size());
                _ce[chinese] = english;
                _ce[english] = chinese;
            }
        }

        file.close(); 
    }

    std::string Translate(const std::string s)
    {
        auto pos = _ce.find(s);
        if(pos == _ce.end())
        {
            return "不存在";
        }
        
        return pos->second;
    }

    ~Dict() = default;
private:
    std::unordered_map<std::string, std::string> _ce;
};